﻿Imports TNCDAL
Public Class ucRoleMenu
    Private ctrlMenu As New CMenuController
    Private ctrlRoleMenu As New CRoleMenuController
    Private _curIDRole As Integer = -1
    Private Sub ucRoleMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindMenuToCheckBox()
        CViewer.BindRoleToComboBox(cboRole)
        'If (cboRole.Items.Count > 0) Then
        '    cboRole.SelectedIndex = 0
        'End If
    End Sub

    Private Sub cboRole_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRole.SelectedIndexChanged
        If (cboRole.SelectedIndex <> -1) Then
            Try
                _curIDRole = cboRole.SelectedValue
                CapNhatMenuTheoNhom()
            Catch ex As Exception
                UnSellectAll()
            End Try
        Else
            UnSellectAll()
        End If
    End Sub
    Private Sub UnSellectAll()
        For i As Integer = 0 To chkLvMenu.Items.Count - 1
            chkLvMenu.SetItemChecked(i, False)
        Next
    End Sub
    Private Sub SellectAll()
        For i As Integer = 0 To chkLvMenu.Items.Count - 1
            chkLvMenu.SetItemChecked(i, True)
        Next
    End Sub
    Private Sub BindMenuToCheckBox()
        chkLvMenu.Items.Clear()
        Dim listMenu = ctrlMenu.SelectAllRecord
        For Each m As tblMenu In listMenu
            chkLvMenu.Items.Add(m.MenuText)
        Next
    End Sub
    Private Sub CapNhatMenuTheoNhom()
        'bỏ tất cả các lựa chọn trước
        UnSellectAll()
        'set lại check theo role
        If (_curIDRole <> -1) Then
            For i As Integer = 0 To chkLvMenu.Items.Count - 1
                Dim menuName As String = chkLvMenu.Items(i).ToString()
                If ctrlRoleMenu.CheckMenuInRoleByName(_curIDRole, menuName) = True Then
                    chkLvMenu.SetItemChecked(i, True)
                End If
            Next
        End If
    End Sub

    Private Sub btUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUpdate.Click
        If (cboRole.SelectedIndex <> -1) Then
            'xóa hết các quyền cũ đi
            ctrlRoleMenu.DeleteAllByRole(_curIDRole)
            'cập nhật quyền mới
            Dim checked_items As CheckedListBox.CheckedItemCollection = chkLvMenu.CheckedItems
            For Each item As Object In checked_items
                Dim menuText As String = item.ToString()
                Dim menu As tblMenu = ctrlMenu.SelectRecordByName(menuText)
                Dim roleMenu As New tblRoleMenu
                roleMenu.ID_Role = cboRole.SelectedValue
                roleMenu.ID_Menu = menu.ID_Menu
                ctrlRoleMenu.InsertRecord(roleMenu)
            Next item
            MessageBox.Show("Đã cập nhật quyền cho nhóm!")
        End If
    End Sub
    Private Sub btCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancel.Click
        BindMenuToCheckBox()
        CapNhatMenuTheoNhom()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        'lấy tất cả menu trên form Main
        For Each tabItem As DevComponents.DotNetBar.RibbonTabItem In frmMain.RibbonControl1.Items
            Dim panel As DevComponents.DotNetBar.RibbonPanel = tabItem.Panel
            For Each bar As DevComponents.DotNetBar.RibbonBar In panel.Controls
                For Each item As DevComponents.DotNetBar.BaseItem In bar.Items
                    Try
                        'kiểm tra xem nó đã tồn tại chưa
                        'nếu chưa có thì insert nó vào dữ liệu
                        If (ctrlMenu.CheckExists(item.Text, -1) = False) Then
                            Dim menu As New tblMenu
                            menu.MenuName = item.Name
                            menu.MenuText = item.Text
                            ctrlMenu.InsertRecord(menu)
                        End If
                    Catch ex As Exception
                    End Try
                Next

            Next
        Next
        'sau khi insert xong load lại
        BindMenuToCheckBox()
        CapNhatMenuTheoNhom()
    End Sub

    Private Sub btCheckAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCheckAll.Click
        SellectAll()
    End Sub

    Private Sub btUnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUnCheck.Click
        UnSellectAll()
    End Sub
End Class
